import matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import pandas as pd

def upsample(X,y):
    dataset=np.append(X,y.reshape(-1,1),axis=1)
    s_data=np.array(sorted(dataset, key=lambda x: x[-1]))
    data=s_data[:,:-1].tolist()
    labels=s_data[:,-1].tolist()

    X=X.tolist()
    y=y.tolist()
    for i in range(0,s_data.shape[0]):
        if s_data[i][-1]>0.87:
            break

        else:
            for j in range(10):
                # X.append(s_data[i][:-1])
                # y.append(s_data[i][-1])
                newLine_X=[None for _ in range(0,14)]
                newLine_X[0]=round(X[i][0]+(np.random.random()*2-1)*0.1,2)
                newLine_X[1]=round(X[i][1]+(np.random.random()*2-1)*0.1,2)
                newLine_X[2]=round(X[i][2]+(np.random.random()*2-1)*0.1,2)
                newLine_X[3]=round(X[i][3]+(np.random.random()*2-1)*0.1,2)
                newLine_X[4]=round(X[i][4]+(np.random.random()*2-1)*0.1,2)
                newLine_X[5]=round(X[i][5]+(np.random.random()*2-1)*0.1,2)
                newLine_X[6]=round(X[i][6]+(np.random.random()*2-1)*0.1,2)
                newLine_X[7]=round(X[i][7]+(np.random.random()*2-1)*0.1,2)
                newLine_X[8]=round(X[i][8]+(np.random.random()*2-1)*0.01,3)
                newLine_X[9]=round(X[i][9]+(np.random.random()*2-1)*0.01,3)
                newLine_X[10]=round(X[i][10]+(np.random.random()*2-1)*0.01,3)
                newLine_X[11]=round(X[i][11]+(np.random.random()*2-1)*0.1,2)
                newLine_X[12]=round(X[i][12]+(np.random.random()*2-1)*0.01,2)
                newLine_X[13]=round(X[i][13]+(np.random.random()*2-1)*0.01,2)
                newLine_y=round(y[i]+(np.random.random()*2-1)*0.001,3)
                X.append(newLine_X)
                y.append(newLine_y)

        # if i!=0:
        #     X_new = [b - a for a, b in zip(s_data[i, :-1], s_data[i - 1, :-1])]
        #     y_new = s_data[i][-1]-s_data[i-1][-1]
        #     randomNum_X=np.random.random()
        #     X_new = [randomNum_X * a for a in X_new]
        #     randomNum_y=np.random.random()
        #     y_new = randomNum_y * y_new
        #     X_new=[a+b for a, b in zip(s_data[i,:-1], X_new)]
        #     y_new=s_data[i][-1]+y_new
        #
        #     X.append(X_new)
        #     y.append(y_new)
        #
        # if i<s_data.shape[0]-1:
        #     X_new = [b - a for a, b in zip(s_data[i,:-1], s_data[i+1,:-1])]
        #     y_new = s_data[i][-1]-s_data[i+1][-1]
        #     randomNum_X=np.random.random()
        #     X_new = [randomNum_X * a for a in X_new]
        #     randomNum_y=np.random.random()
        #     y_new = randomNum_y * y_new
        #     X_new=[a+b for a, b in zip(s_data[i,:-1], X_new)]
        #     y_new=s_data[i][-1]+y_new
        #
        #     X.append(X_new)
        #     y.append(y_new)

        # k=1
        # while i-k>=0 and abs(s_data[i][-1]-s_data[i-k][-1])<=0.01:
        #     X_new = [b - a for a, b in zip(s_data[i,:-1], s_data[i-k,:-1])]
        #     y_new = s_data[i][-1]-s_data[i-k][-1]
        #     randomNum_X=np.random.random()
        #     X_new = [randomNum_X * a for a in X_new]
        #     randomNum_y=np.random.random()
        #     y_new = randomNum_y * y_new
        #     X_new=[a+b for a, b in zip(s_data[i,:-1], X_new)]
        #     y_new=s_data[i][-1]+y_new
        #
        #     X.append(X_new)
        #     y.append(y_new)
        #
        #     k+=1
        # k=1
        # while i+k<s_data.shape[0] and abs(s_data[i][-1]-s_data[i+k][-1])<=0.01:
        #     X_new = [b - a for a, b in zip(s_data[i,:-1], s_data[i+k,:-1])]
        #     y_new = s_data[i][-1]-s_data[i+k][-1]
        #     randomNum_X=np.random.random()
        #     X_new = [randomNum_X * a for a in X_new]
        #     randomNum_y=np.random.random()
        #     y_new = randomNum_y * y_new
        #     X_new=[a+b for a, b in zip(s_data[i,:-1], X_new)]
        #     y_new=s_data[i][-1]+y_new
        #
        #     X.append(X_new)
        #     y.append(y_new)
        #
        #     k+=1


    return np.array(X), np.array(y)







    # for i in range(0,y.shape[0]):
    #     if y[i]<0.87:
    #         for j in range(0,3):
    #             newLine_X=[None for _ in range(0,14)]
    #             newLine_X=X[i]
    #             # newLine_X[0]=round(X[i][0]+(np.random.random()*2-1)*0.1,2)
    #             # newLine_X[1]=round(X[i][1]+(np.random.random()*2-1)*0.1,2)
    #             # newLine_X[2]=round(X[i][2]+(np.random.random()*2-1)*0.1,2)
    #             # newLine_X[3]=round(X[i][3]+(np.random.random()*2-1)*0.1,2)
    #             # newLine_X[4]=round(X[i][4]+(np.random.random()*2-1)*0.1,2)
    #             # newLine_X[5]=round(X[i][5]+(np.random.random()*2-1)*0.1,2)
    #             # newLine_X[6]=round(X[i][6]+(np.random.random()*2-1)*0.1,2)
    #             # newLine_X[7]=round(X[i][7]+(np.random.random()*2-1)*0.1,2)
    #             # newLine_X[8]=round(X[i][8]+(np.random.random()*2-1)*0.01,3)
    #             # newLine_X[9]=round(X[i][9]+(np.random.random()*2-1)*0.01,3)
    #             # newLine_X[10]=round(X[i][10]+(np.random.random()*2-1)*0.01,3)
    #             # newLine_X[11]=round(X[i][11]+(np.random.random()*2-1)*0.1,2)
    #             # newLine_X[12]=round(X[i][12]+(np.random.random()*2-1)*0.01,2)
    #             # newLine_X[13]=round(X[i][13]+(np.random.random()*2-1)*0.01,2)
    #             # newLine_y=round(y[i]+(np.random.random()*2-1)*0.001,3)
    #             newLine_y=y[i]
    #             data.append(newLine_X)
    #             labels.append(newLine_y)
    #
    # return np.array(data),np.array(labels)